package com.yyy.gui.pannel;

import com.yyy.entity.Category;
import com.yyy.gui.listener.RecordListener;
import com.yyy.gui.model.CategoryComboBoxModel;
import com.yyy.service.CategoryService;
import com.yyy.util.ColorUtil;
import com.yyy.util.GUIUtil;
import org.jdesktop.swingx.JXDatePicker;

import javax.swing.*;
import java.awt.*;
import java.util.Date;


/**
 * @Author：yyy
 * @Package：com.yyy.gui.pannel
 * @Project：HuTuBiill
 * @name：RecordPannel
 * @Date：2024/11/9 20:43
 * @Filename：RecordPannel
 */
public class RecordPannel extends WorkingPanel{
    public static RecordPannel instance=new RecordPannel();
    public JLabel lSpend=new JLabel("花费(￥)");
    public JLabel lCategory=new JLabel("分类");
    public JLabel lComment=new JLabel("备注");
    public JLabel lDate=new JLabel("日期");
    public JTextField tfSpend=new JTextField("0");
    public CategoryComboBoxModel cbModel=new CategoryComboBoxModel();
    public JComboBox<Category> cbCategory=new JComboBox<Category>(cbModel);
    public JTextField tfComment=new JTextField();
    public JXDatePicker datePicker=new JXDatePicker(new Date());
    public JButton bSubmit=new JButton("记一笔");

    public RecordPannel(){
        GUIUtil.setColor(ColorUtil.grayColor,lSpend,lCategory,lComment,lDate);
        GUIUtil.setColor(ColorUtil.blueColor,bSubmit);
        JPanel pInput=new JPanel();
        JPanel pSubmit=new JPanel();
        int gap=40;
        pInput.setLayout(new GridLayout(4,2,gap,gap));
        pInput.add(lSpend);
        pInput.add(tfSpend);
        pInput.add(lCategory);
        pInput.add(cbCategory);
        pInput.add(lComment);
        pInput.add(tfComment);
        pInput.add(lDate);
        pInput.add(datePicker);

        pSubmit.add(bSubmit);

        this.setLayout(new BorderLayout());
        this.add(pInput,BorderLayout.NORTH);
        this.add(pSubmit,BorderLayout.SOUTH);
        addListener();
    }
    public Category getSelectedCategory(){
        return (Category) cbCategory.getSelectedItem();
    }

    public void resetInput(){
        tfSpend.setText("0");
        tfComment.setText("");
        if(0!=cbModel.cs.size())
            cbCategory.setSelectedIndex(0);
        datePicker.setDate(new Date());
    }


    @Override
    public void addListener() {
        RecordListener recordListener=new RecordListener();
        bSubmit.addActionListener(recordListener);


    }

    @Override
    public void updateData() {
        cbModel.cs=new CategoryService().list();
        cbCategory.updateUI();
        resetInput();//让各个输入框信息重置
        tfSpend.grabFocus();//聚焦在金额的输入框上



    }

    public static void main(String[] args) {
        GUIUtil.showPanel(instance,1);
    }

}
